home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.12 Dec 90 / NeXT for Mac Code / RocketView.m < prev   
Encoding:
Text File  |  1990-03-26  |  2.8 KB  |  133 lines  |  [TEXT/QED1]

  1. /* FILE = RocketView.m */
  2.  
  3. #import <appkit/appkit.h>
  4. #import "rocketview.h"
  5.  
  6. @implementation  RocketView : View
  7. {    
  8.     /* repeating the instance-variables part of the object declaration is not 
  9. required by the syntax, but it is permitted and checked to match the interface 
  10. declaration, and I think it is a good idea for documentation purposes. */
  11.  
  12.     NXPoint    misslePositions[MAXMISSLES];
  13.     BOOL        misslesLaunched;
  14.     int        numRockets;
  15.  
  16.     id        BlaunchRockets; 
  17.     /* launch rocket button */
  18.  
  19.     id        IFnumberRockets; 
  20.     /* input text field = a Form object */
  21. }
  22.  
  23. + newFrame: (const NXRect *) frameRect
  24.     /* override View's default newFrame method */
  25. {
  26.     int i;
  27.  
  28.     self = [ super newFrame: frameRect ]; 
  29.  
  30. /* change self to point to the newly created object-instance, instead of 
  31. pointing to the rocketView Factory object. */
  32.  
  33.     numRockets = 0;
  34.     for ( i = 0; i < MAXMISSLES ; i++ )
  35.     {
  36.         misslePositions[i].x = (1+i) * 10;
  37.         misslePositions[i].y = 10.0;
  38.     }
  39.     misslesLaunched = NO;
  40.  
  41.     [self setFlip: NO]; 
  42.     /* keep 0,0 at lower left corner of view */
  43.         
  44.     [self allocateGstate];
  45.     return self; 
  46. }
  47.  
  48. - updatedrawing
  49.     /* draws the missles in the view */
  50. {
  51.     int i;
  52.  
  53.     [self lockFocus ];
  54.  
  55.     if ( misslesLaunched )
  56.     {
  57.         for (i = 0; i < numRockets - 1; i++ )
  58.         {    
  59.             /* draw Very simple missles — not 
  60.                 even animated! */
  61.             PSmoveto( misslePositions[i].x, 
  62.                 misslePositions[i].y );
  63.             PSlineto( misslePositions[i].x, 
  64.                 misslePositions[i].y + 10.0 );
  65.             PSstroke();
  66.         }
  67.     }
  68.     NXPing(); 
  69.  
  70.     /* NXPing forces the window manager to update the view on screen -- flushes 
  71. the postscript pipeline */
  72.  
  73.     [self unlockFocus];
  74.  
  75.     return self; 
  76.  
  77.     /* if nothing else, tradition says return self */
  78. }
  79.  
  80. - drawSelf: (const NXRect *) rects : (int) rectCount
  81.     /* override View's default drawing method */
  82. {
  83.     /* ignore the rectangles which could be used for limiting the amount of 
  84. drawing required. */
  85.  
  86.     [ self updatedrawing ];
  87.     return self;
  88. }
  89.  
  90. - setBlaunchRockers:anObject
  91.     /* created by interface builder */
  92. {
  93.     BlaunchRockers = anObject;
  94.     return self;
  95. }
  96.  
  97. - setIFnumberRockets:anObject
  98.     /* created by interface builder */
  99. {
  100.     IFnumberRockets = anObject;
  101.     return self;
  102. }
  103.  
  104. - rocketlaunch:sender
  105.     /* in this program, this sender will always be the button named "Launch 
  106. Rockets" and this routine will be called when the user clicks in this button 
  107. object during execution of this program */
  108. {
  109.     int i;
  110.  
  111.     if ( ! misslesLaunched )
  112.     {
  113.         misslesLaunched = YES;
  114.         i = [ IFnumberRockets intValueAt: 0 ];
  115.  
  116.         /* get the number of rockets from the string contained in the Form.  We 
  117. specified an initial default value with Interface Builder; at any time before 
  118. pushing the button, the user could change the text to any string.  If an integer 
  119. can not be parsed from the string, we get zero as the value. */
  120.             
  121.         if ( i <= MAXMISSLES )
  122.         {
  123.             numRockets = i;
  124.         }
  125.     }
  126.     else
  127.     {
  128.         NXBeep();
  129.     }
  130.     [ self updatedrawing ];
  131. }
  132.  
  133. @end